package com.example.kuaizu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.kuaizu.mapper.plus.MagorMapper;
import com.example.kuaizu.mapper.plus.MagorMapperClassMapper;
import kuaizu.common.tablepojo.Magor;
import kuaizu.common.tablepojo.MagorClass;
import kuaizu.common.tablepojo.School;
import com.example.kuaizu.pojo.dto.SchoolCountObject;
import com.example.kuaizu.service.InSchoolService;
import com.example.kuaizu.utils.AjaxResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@RestController
@RequestMapping("/school")
@RequiredArgsConstructor
public class SchoolController {
    @Autowired
    private InSchoolService inSchoolService;
    private final MagorMapper magorMapper;
    private final MagorMapperClassMapper magorMapperClassMapper;

    //查询某个学校的注册人数
    @GetMapping("/getSchoolRegisterCount")
    public AjaxResponse getSchoolRegisterCount() {
        log.info("查询某个学校的注册人数");
        List<SchoolCountObject> list = inSchoolService.getSchoolRegisterCount();
        return AjaxResponse.success("查询成功", list);
    }

    //根据学校id查询学校信息
    @GetMapping("/getSchoolById/{id}")
    public AjaxResponse getSchoolById(@PathVariable Integer id) {
        log.info("根据学校id查询学校信息");
        School school = inSchoolService.selectSchoolByIdFromSchoolName(id);
        return AjaxResponse.success("查询成功", school);
    }

    //根据查找专业信息
    @GetMapping("/getMajor")
    public AjaxResponse getMajor() {
        return AjaxResponse.success("查询成功", magorMapper.selectList(null));
    }

    //查询专业大类
    @GetMapping("/getMajorClass")
    public AjaxResponse getMajorClass() {
        return AjaxResponse.success("查询成功", magorMapperClassMapper.selectList(null));
    }

    //根据专业大类查询专业
    @GetMapping("/getMajorByMajorClassId/{majorClassId}")
    public AjaxResponse getMajorByMajorClassId(@PathVariable Integer majorClassId) {
        return AjaxResponse.success("查询成功", magorMapper.selectList(new
                QueryWrapper<Magor>().eq("major_class_id", majorClassId)));
    }

    //根据id查询专业名称
    @GetMapping("/getMajorNameById/{id}")
    public AjaxResponse getMajorNameById(@PathVariable Integer id) {
        return AjaxResponse.success("查询成功", magorMapper.selectById(id));
    }

    //根据id查询专业大类
    @GetMapping("/getMajorClassById/{id}")
    public AjaxResponse getMajorClassById(@PathVariable Integer id) {
        return AjaxResponse.success("查询成功", magorMapperClassMapper.selectById(id));
    }

    //根据文字模糊匹配专业名称,返回类似的专业集合名称
    @GetMapping("/getMajorByName")
    public AjaxResponse getMajorByName(String name) {
        return AjaxResponse.success("查询成功", magorMapper.selectList(new
                QueryWrapper<Magor>().like("major_name", name)));
    }

    //根据文字模糊匹配专业大类名称
    @GetMapping("/getMajorClassByName")
    public AjaxResponse getMajorClassByName(String name) {
        return AjaxResponse.success("查询成功", magorMapperClassMapper.selectList(new
                QueryWrapper<MagorClass>().like("major_class", name)));
    }

//根据学校id查询对应的学生信息
    @GetMapping("/getStudentBySchoolId/{schoolId}")
    public AjaxResponse getStudentBySchoolId(@PathVariable Integer schoolId) {
        return AjaxResponse.success("查询成功", inSchoolService.getStudentBySchoolId(schoolId));
    }

    /**
     * 查询指定学校下的所有专业的人数
     */
    @GetMapping("/getMajorBySchoolId/{schoolId}")
    public AjaxResponse getMajorBySchoolId(@PathVariable Integer schoolId) {
        return AjaxResponse.success("查询成功", inSchoolService.getMajorBySchoolId(schoolId));
    }

    /**
     * 查询所有专业注册人数
     */
    @GetMapping("/getMajorRegisterCount")
    public AjaxResponse getMajorRegisterCount() {
        return AjaxResponse.success("查询成功", inSchoolService.getMajorRegisterCount());
    }

}
